// This template will be used for displaying the MCQ to the student.
// The window will display a question, 4 options and a hint. Student 
// should be able to pick one or more options and submit.
// author vinil.bhandari@kaplan.com
Titanium.include("/db/db.js");
var win = Titanium.UI.currentWindow;
win.backgroundColor = '#B6CCDD';

//Get DB object
var dbase = Titanium.Database.open('gyaan');
// Create a TableView row for each question in the database table
var myResultSet = dbase.execute('SELECT * FROM questions');
var data = [];

while (myResultSet.isValidRow()) {
	var chosenAns = myResultSet.fieldByName('answer_provided');
	chosenAns= chosenAns=="0"?'A':chosenAns=="1"?'B':chosenAns=="2"?'C':chosenAns=="3"?'D':chosenAns=="4"?'E':'';
	data.push({title:'Ans'+myResultSet.fieldByName('id')+': Chosen - '+ chosenAns+', Correct - '+myResultSet.fieldByName('actual_answer')});
	myResultSet.next();
}
myResultSet.close();

var tableView1 = Titanium.UI.createTableView({
    data:data,
    style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
    bottom: 50,
    top:50
});	

dbase.close();
win.add(tableView1);



var userType = Titanium.App.Properties.getString("userType").toUpperCase();

	var flexSpace = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});

	

if(userType=="TEACHER"){
var emailField = Titanium.UI.createTextField({
	hintText:'Enter comma seperated emails of students',
	height:40,
	top:15,
	right:5,
	width:350,
	zIndex:2,
	borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});

win.add(emailField);
	
	
	
	var b9 = Titanium.UI.createButton({
		title:'Send to students',
		style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED,
		color:'#463E77',
		backgroundColor:'#463E77'
	});	
b9.addEventListener('click', function() {
	if(emailField.value=="" || emailField.value==null){
		alert("Please provide valid email addresses for sharing.");
	}else{
	
	var emailDialog = Titanium.UI.createEmailDialog();
	emailDialog.subject = "New Assignment";
	emailDialog.toRecipients = [emailField.value];
	emailDialog.messageBody ="You have a new assignment from your teacher. Please log in to OneLove application to access it.";
	emailDialog.addEventListener('complete', function(e) {
	var dbase = Titanium.Database.open('gyaan');
	// Create a TableView row for each question in the database table
	var myResultSet = dbase.execute('SELECT q.title as title,q.actual_answer as actual_answer, o.a as a, o.b as b, o.c as c, o.d as d, o.e as e  FROM questions q, options o where o.q_id=q.id');
	var data = [];
	var count =1;
	while (myResultSet.isValidRow()) {
		if(count>3){
	    	data[0] =myResultSet.fieldByName('title');
	    	data[1] ='a:'+myResultSet.fieldByName('a')+","+'b:'+myResultSet.fieldByName('b')+","+'c:'+myResultSet.fieldByName('c')+","+'d:'+myResultSet.fieldByName('d')+","+'e:'+myResultSet.fieldByName('e');
	    	data[2] ='random';
	    	data[3] =myResultSet.fieldByName('actual_answer');
	    	data[4] ='img';
	    	data[5] =emailDialog.getToRecipients();
	    	createQuestion(data, count);
	    	data = [];
    	}
		count++;
		myResultSet.next();
	}
	
	myResultSet.close();
	
	var tableView1 = Titanium.UI.createTableView({
	    data:data,
	    style:Titanium.UI.iPhone.TableViewStyle.GROUPED
	    
	});	
	
	dbase.close();	
		if (e.result == emailDialog.SENT) {
			Titanium.API.info("To>"+emailDialog.getToRecipients());
			Titanium.UI.createAlertDialog({
				title : 'Message Sent',
				message :'An email notification has been sent to your students. Sit back relax and wait for their responses. You can view the progress of your students on your dashboard'
			}).show();
		}
	});
	emailDialog.open();
	}
}); 

}
else{
	
	var b9 = Titanium.UI.createButton({
		title:'Send to teacher',
		style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED,
		color:'#463E77',
		backgroundColor:'#463E77'
	});
	b9.addEventListener('click', function() {
	var emailDialog = Titanium.UI.createEmailDialog();
	emailDialog.subject = "Upload Assignment";
	emailDialog.toRecipients = [''];
	emailDialog.messageBody ="I have finished my assignment please view my progress";
	emailDialog.addEventListener('complete', function(e) {
	
	var dbase = Titanium.Database.open('gyaan');
	// Create a TableView row for each question in the database table
	var myResultSet = dbase.execute('SELECT q.answer_provided as answer_provided, q.q_id as q_id FROM questions q');
	var data = [];
	var count =1;

	while (myResultSet.isValidRow()) {
		if(count>3){
	    	data[0] =myResultSet.fieldByName('answer_provided');
	    	data[1] =myResultSet.fieldByName('q_id');
	    	submitAnswers(data);
	    	data = [];
    	}
		count++;
		myResultSet.next();
	}
	
	myResultSet.close();
	
	var tableView1 = Titanium.UI.createTableView({
	    data:data,
	    style:Titanium.UI.iPhone.TableViewStyle.GROUPED
	    
	});	
	
	dbase.close();	
		if (e.result == emailDialog.SENT) {
			Titanium.UI.createAlertDialog({
				title : 'Message Sent',
				message : '',
			}).show();
		}
	});
	emailDialog.open();
}); 
}
	win.setToolbar([flexSpace,flexSpace,b9],{translucent:true});
